package model

import (
	"time"
)

// CourseReview 课程评价表
type CourseReview struct {
	ID           uint      `gorm:"primaryKey" json:"id"`
	CourseID     uint      `gorm:"not null;index" json:"courseId"`
	BookingID    *uint     `gorm:"index" json:"bookingId"`
	UserID       uint      `gorm:"not null;index" json:"userId"`
	Rating       int       `gorm:"not null" json:"rating"` // 评分（1-5星）
	Content      string    `gorm:"type:text" json:"content"`
	Images       string    `gorm:"type:json" json:"images"` // 评价图片URL列表，JSON格式存储
	Tags         string    `gorm:"type:json" json:"tags"`   // 标签，JSON格式存储
	IsAnonymous  bool      `gorm:"default:false" json:"isAnonymous"`
	IsFeatured   bool      `gorm:"default:false" json:"isFeatured"`
	HelpfulCount int       `gorm:"default:0" json:"helpfulCount"`
	Status       string    `gorm:"type:varchar(20);default:'PENDING'" json:"status"` // PENDING/APPROVED/REJECTED/HIDDEN
	RejectReason string    `gorm:"type:varchar(500)" json:"rejectReason"`
	CreatedAt    time.Time `json:"createdAt"`
	UpdatedAt    time.Time `json:"updatedAt"`

	// 关联字段
	User   *User   `gorm:"foreignKey:UserID" json:"user,omitempty"`
	Course *Course `gorm:"foreignKey:CourseID" json:"course,omitempty"`
}

// CoachRating 教练评分表
type CoachRating struct {
	ID              uint      `gorm:"primaryKey" json:"id"`
	CoachID         uint      `gorm:"not null;index" json:"coachId"`
	UserID          uint      `gorm:"not null" json:"userId"`
	BookingID       *uint     `gorm:"index" json:"bookingId"`
	OverallRating   float64   `gorm:"type:decimal(2,1);not null" json:"overallRating"`
	Professionalism *int      `json:"professionalism"` // 专业度评分（1-5）
	Patience        *int      `json:"patience"`        // 耐心度评分（1-5）
	Enthusiasm      *int      `json:"enthusiasm"`      // 热情度评分（1-5）
	Communication   *int      `json:"communication"`   // 沟通能力评分（1-5）
	Content         string    `gorm:"type:text" json:"content"`
	IsAnonymous     bool      `gorm:"default:false" json:"isAnonymous"`
	CreatedAt       time.Time `json:"createdAt"`
	UpdatedAt       time.Time `json:"updatedAt"`

	// 关联字段
	User  *User  `gorm:"foreignKey:UserID" json:"user,omitempty"`
	Coach *Coach `gorm:"foreignKey:CoachID" json:"coach,omitempty"`
}

// Feedback 意见反馈表
type Feedback struct {
	ID           uint       `gorm:"primaryKey" json:"id"`
	UserID       uint       `gorm:"not null;index" json:"userId"`
	Type         string     `gorm:"type:varchar(20);not null" json:"type"` // BUG/FEATURE/COMPLAINT/SUGGESTION/OTHER
	Category     string     `gorm:"type:varchar(50)" json:"category"`      // 分类（课程/设施/服务/系统等）
	Title        string     `gorm:"size:200;not null" json:"title"`
	Content      string     `gorm:"type:text;not null" json:"content"`
	Images       string     `gorm:"type:json" json:"images"` // 附件图片URL列表，JSON格式存储
	ContactPhone string     `gorm:"type:varchar(20)" json:"contactPhone"`
	ContactEmail string     `gorm:"type:varchar(100)" json:"contactEmail"`
	Priority     string     `gorm:"type:varchar(20);default:'NORMAL'" json:"priority"` // LOW/NORMAL/HIGH/URGENT
	Status       string     `gorm:"type:varchar(20);default:'PENDING'" json:"status"`  // PENDING/PROCESSING/RESOLVED/CLOSED/REJECTED
	HandlerID    *uint      `json:"handlerId"`
	Reply        string     `gorm:"type:text" json:"reply"`
	ResolvedAt   *time.Time `json:"resolvedAt"`
	CreatedAt    time.Time  `json:"createdAt"`
	UpdatedAt    time.Time  `json:"updatedAt"`

	// 关联字段
	User    *User `gorm:"foreignKey:UserID" json:"user,omitempty"`
	Handler *User `gorm:"foreignKey:HandlerID" json:"handler,omitempty"`
}

// ReviewReply 评价回复表
type ReviewReply struct {
	ID         uint      `gorm:"primaryKey" json:"id"`
	ReviewID   uint      `gorm:"not null;index" json:"reviewId"`
	ReviewType string    `gorm:"type:varchar(20);not null" json:"reviewType"` // COURSE/COACH
	UserID     uint      `gorm:"not null;index" json:"userId"`
	UserType   string    `gorm:"type:varchar(20);not null" json:"userType"` // COACH/ADMIN/MEMBER
	Content    string    `gorm:"type:text;not null" json:"content"`
	IsOfficial bool      `gorm:"default:false" json:"isOfficial"`
	CreatedAt  time.Time `json:"createdAt"`
	UpdatedAt  time.Time `json:"updatedAt"`

	// 关联字段
	User *User `gorm:"foreignKey:UserID" json:"user,omitempty"`
}

// ReviewHelpful 评价有用记录表
type ReviewHelpful struct {
	ID        uint      `gorm:"primaryKey" json:"id"`
	ReviewID  uint      `gorm:"not null" json:"reviewId"`
	UserID    uint      `gorm:"not null" json:"userId"`
	CreatedAt time.Time `json:"createdAt"`

	// 关联字段
	User *User `gorm:"foreignKey:UserID" json:"user,omitempty"`
}

// ==================== 请求/响应模型 ====================

// CreateCourseReviewReq 创建课程评价请求
type CreateCourseReviewReq struct {
	CourseID    uint     `json:"courseId" binding:"required"`
	BookingID   *uint    `json:"bookingId"`
	Rating      int      `json:"rating" binding:"required,min=1,max=5"`
	Content     string   `json:"content" binding:"required,min=10"`
	Tags        []string `json:"tags"`
	Images      []string `json:"images"`
	IsAnonymous bool     `json:"isAnonymous"`
}

// CourseReviewListReq 课程评价列表查询请求
type CourseReviewListReq struct {
	CourseID  uint   `form:"courseId" binding:"required"`
	Rating    *int   `form:"rating"`
	HasImages *bool  `form:"hasImages"`
	Sort      string `form:"sort"` // latest/rating/helpful
	Page      int    `form:"page" binding:"required,min=1"`
	PageSize  int    `form:"pageSize" binding:"required,min=1,max=100"`
}

// CourseReviewListResp 课程评价列表响应
type CourseReviewListResp struct {
	Summary struct {
		TotalCount         int           `json:"totalCount"`
		AvgRating          float64       `json:"avgRating"`
		RatingDistribution map[int]int64 `json:"ratingDistribution"`
	} `json:"summary"`
	List  []CourseReviewDetail `json:"list"`
	Total int64                `json:"total"`
	Page  int                  `json:"page"`
	Size  int                  `json:"size"`
}

// CourseReviewDetail 课程评价详情
type CourseReviewDetail struct {
	ID           uint          `json:"id"`
	User         UserInfo      `json:"user"`
	Rating       int           `json:"rating"`
	Content      string        `json:"content"`
	Images       []string      `json:"images"`
	Tags         []string      `json:"tags"`
	HelpfulCount int           `json:"helpfulCount"`
	IsHelpful    bool          `json:"isHelpful"`
	Replies      []ReviewReply `json:"replies"`
	CreatedAt    time.Time     `json:"createdAt"`
}

// CreateCoachRatingReq 创建教练评分请求
type CreateCoachRatingReq struct {
	CoachID         uint    `json:"coachId" binding:"required"`
	BookingID       *uint   `json:"bookingId"`
	OverallRating   float64 `json:"overallRating" binding:"required,min=0,max=5"`
	Professionalism *int    `json:"professionalism"`
	Patience        *int    `json:"patience"`
	Enthusiasm      *int    `json:"enthusiasm"`
	Communication   *int    `json:"communication"`
	Content         string  `json:"content"`
	IsAnonymous     bool    `json:"isAnonymous"`
}

// CoachRatingResp 教练评分响应
type CoachRatingResp struct {
	ID            uint    `json:"id"`
	OverallRating float64 `json:"overallRating"`
}

// CoachRatingListReq 教练评分列表查询请求
type CoachRatingListReq struct {
	CoachID  uint `form:"coachId" binding:"required"`
	Page     int  `form:"page" binding:"required,min=1"`
	PageSize int  `form:"pageSize" binding:"required,min=1,max=100"`
}

// CoachRatingListResp 教练评分列表响应
type CoachRatingListResp struct {
	Summary struct {
		TotalCount         int64   `json:"totalCount"`
		AvgOverallRating   float64 `json:"avgOverallRating"`
		AvgProfessionalism float64 `json:"avgProfessionalism"`
		AvgPatience        float64 `json:"avgPatience"`
		AvgEnthusiasm      float64 `json:"avgEnthusiasm"`
		AvgCommunication   float64 `json:"avgCommunication"`
	} `json:"summary"`
	List  []CoachRatingDetail `json:"list"`
	Total int64               `json:"total"`
}

// CoachRatingDetail 教练评分详情
type CoachRatingDetail struct {
	ID            uint      `json:"id"`
	User          UserInfo  `json:"user"`
	OverallRating float64   `json:"overallRating"`
	Content       string    `json:"content"`
	CreatedAt     time.Time `json:"createdAt"`
}

// CreateFeedbackReq 创建意见反馈请求
type CreateFeedbackReq struct {
	Type         string   `json:"type" binding:"required,oneof=BUG FEATURE COMPLAINT SUGGESTION OTHER"`
	Category     string   `json:"category"`
	Title        string   `json:"title" binding:"required"`
	Content      string   `json:"content" binding:"required"`
	Images       []string `json:"images"`
	ContactPhone string   `json:"contactPhone"`
	ContactEmail string   `json:"contactEmail"`
}

// FeedbackListReq 意见反馈列表查询请求
type FeedbackListReq struct {
	Type     string `form:"type"`
	Status   string `form:"status"`
	Priority string `form:"priority"`
	Page     int    `form:"page" binding:"required,min=1"`
	PageSize int    `form:"pageSize" binding:"required,min=1,max=100"`
}

// FeedbackListResp 意见反馈列表响应
type FeedbackListResp struct {
	List  []Feedback `json:"list"`
	Total int64      `json:"total"`
	Page  int        `json:"page"`
	Size  int        `json:"size"`
}

// HandleFeedbackReq 处理意见反馈请求
type HandleFeedbackReq struct {
	Status string `json:"status" binding:"required,oneof=RESOLVED CLOSED REJECTED"`
	Reply  string `json:"reply"`
}

// AuditReviewReq 审核评价请求
type AuditReviewReq struct {
	Status       string `json:"status" binding:"required,oneof=APPROVED REJECTED HIDDEN"`
	RejectReason string `json:"rejectReason"`
}

// FeatureReviewReq 精选评价请求
type FeatureReviewReq struct {
	IsFeatured bool `json:"isFeatured"`
}

// ReviewReport 评价举报表
type ReviewReport struct {
	ID          uint       `gorm:"primaryKey" json:"id"`
	ReviewID    uint       `gorm:"not null;index" json:"reviewId"`
	UserID      uint       `gorm:"not null;index" json:"userId"`
	Reason      string     `gorm:"type:varchar(200);not null" json:"reason"`         // 举报原因
	Description string     `gorm:"type:text" json:"description"`                     // 详细描述
	Status      string     `gorm:"type:varchar(20);default:'PENDING'" json:"status"` // PENDING/RESOLVED/REJECTED
	HandlerID   *uint      `json:"handlerId"`                                        // 处理人ID
	HandledAt   *time.Time `json:"handledAt"`                                        // 处理时间
	CreatedAt   time.Time  `json:"createdAt"`
	UpdatedAt   time.Time  `json:"updatedAt"`

	// 关联字段
	User    *User         `gorm:"foreignKey:UserID" json:"user,omitempty"`
	Review  *CourseReview `gorm:"foreignKey:ReviewID" json:"review,omitempty"`
	Handler *User         `gorm:"foreignKey:HandlerID" json:"handler,omitempty"`
}

// ReportReviewReq 举报评价请求
type ReportReviewReq struct {
	ReviewID    uint   `json:"reviewId" binding:"required"`
	Reason      string `json:"reason" binding:"required,max=200"`
	Description string `json:"description"`
}

// HandleReportReq 处理举报请求
type HandleReportReq struct {
	Status string `json:"status" binding:"required,oneof=RESOLVED REJECTED"`
	Action string `json:"action" binding:"omitempty,oneof=HIDE DELETE"`
}

// ReviewReportListReq 举报列表查询请求
type ReviewReportListReq struct {
	Status   string `form:"status"`
	Page     int    `form:"page" binding:"required,min=1"`
	PageSize int    `form:"pageSize" binding:"required,min=1,max=100"`
}

// ReviewReportListResp 举报列表响应
type ReviewReportListResp struct {
	List  []ReviewReport `json:"list"`
	Total int64          `json:"total"`
	Page  int            `json:"page"`
	Size  int            `json:"size"`
}
